package DAO;

import java.sql.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import modelos.Clientes;

public class ClientesDAO {
	
	public void CadastrarClientes(Clientes cliente) throws SQLException{
		Connection conn = new ConexaoDB().pegarConexao();
		PreparedStatement pstm = conn.prepareStatement("insert into clientes (nome, sexo, dataNascimento, endereco, numero, bairro, cidade, estado, cep, cpf, senha, email, telefone) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");

		
		try{	
			pstm.setString(1, cliente.getNome());
			pstm.setString(2, cliente.getSexo());
			pstm.setDate(3, new java.sql.Date (cliente.getDataNascimento().getTime()));
			pstm.setString(4, cliente.getEndereco());
			pstm.setString(5, cliente.getNumero());
			pstm.setString(6, cliente.getBairro());
			pstm.setString(7, cliente.getCidade());
			pstm.setString(8, cliente.getEstado());
			pstm.setString(9, cliente.getCep());
			pstm.setString(10, cliente.getCpf());
			pstm.setString(11, cliente.getSenha());
			pstm.setString(12, cliente.getEmail());
			pstm.setString(13, cliente.getTelefone());
			
			pstm.execute();
		}
		catch(SQLException ex){
			throw new RuntimeException(ex);
		}
		finally{
			pstm.close();
		}
	}
	
	public void AlterarClientes(Clientes cliente) throws SQLException{
		Connection conn = new ConexaoDB().pegarConexao();
		PreparedStatement pstm = conn.prepareStatement("update clientes set nome = ?, sexo = ?, dataNascimento = ?, endereco = ?, numero = ?, bairro = ?, cidade = ?, estado = ?, cep = ?, cpf = ?, senha = ?, email = ?, telefone = ? where clienteId = ?;");

		try{			
			pstm.setString(1, cliente.getNome());
			pstm.setString(2, cliente.getSexo());
			pstm.setDate(3, new java.sql.Date (cliente.getDataNascimento().getTime()));
			pstm.setString(4, cliente.getEndereco());
			pstm.setString(5, cliente.getNumero());
			pstm.setString(6, cliente.getBairro());
			pstm.setString(7, cliente.getCidade());
			pstm.setString(8, cliente.getEstado());
			pstm.setString(9, cliente.getCep());
			pstm.setString(10, cliente.getCpf());
			pstm.setString(11, cliente.getSenha());
			pstm.setString(12, cliente.getEmail());
			pstm.setString(13, cliente.getTelefone());
			pstm.setInt(14, cliente.getClienteId());
			
			pstm.execute();
			
		}
		catch(SQLException ex){
			throw new RuntimeException(ex);
		}
		finally{
			pstm.close();
		}
	}
	
	public void ExcluirClientes(int id) throws SQLException{
		Connection conn = new ConexaoDB().pegarConexao();
		PreparedStatement pstm = conn.prepareStatement("delete from clientes where clienteId = ?");
		try{
			pstm.setInt(1, id);
			pstm.execute();
		}
		catch(SQLException ex){
			throw new RuntimeException(ex);
		}
		finally{
			pstm.close();
		}
	}
	
	public Collection<Clientes> PesquisarPorID(int id) throws SQLException{
		Connection conn = new ConexaoDB().pegarConexao();
		PreparedStatement pstm = conn.prepareStatement("select * from clientes where clienteId = ?;");
		
		pstm.setInt(1, id);
		ResultSet rs = pstm.executeQuery();
		
		Collection<Clientes> listar = new ArrayList<Clientes>();
		try{
			while(rs.next()){
				Clientes cli = new Clientes();
				cli.setClienteId(rs.getInt(1));
				cli.setNome(rs.getString(2));
				cli.setSexo(rs.getString(3));
				cli.setDataNascimento(rs.getDate(4));
				cli.setEndereco(rs.getString(5));
				cli.setNumero(rs.getString(6));
				cli.setBairro(rs.getString(7));
				cli.setCidade(rs.getString(8));
				cli.setEstado(rs.getString(9));
				cli.setCep(rs.getString(10));
				cli.setCpf(rs.getString(11));
				cli.setEmail(rs.getString(12));
				cli.setSenha(rs.getString(13));
				cli.setTelefone(rs.getString(14));
				
				listar.add(cli);
			}
			return listar;
		}
		catch(SQLException ex){
			throw new RuntimeException(ex);
		}
		finally{
			pstm.close();
		}
	}
	
	public Collection<Clientes> MostrarTudo() throws SQLException{
		Connection conn = new ConexaoDB().pegarConexao();
        Statement stm = conn.createStatement();
        ResultSet rs = stm.executeQuery("select * from clientes;");
        
        Collection<Clientes> listar = new ArrayList<Clientes>();
		while (rs.next()){
			Clientes cli = new Clientes();
			cli.setClienteId(rs.getInt(1));
			cli.setNome(rs.getString(2));
			cli.setSexo(rs.getString(3));
			cli.setDataNascimento(rs.getDate(4));
			cli.setEndereco(rs.getString(5));
			cli.setNumero(rs.getString(6));
			cli.setBairro(rs.getString(7));
			cli.setCidade(rs.getString(8));
			cli.setEstado(rs.getString(9));
			cli.setCep(rs.getString(10));
			cli.setCpf(rs.getString(11));
			cli.setEmail(rs.getString(12));
			cli.setSenha(rs.getString(13));
			cli.setTelefone(rs.getString(14));
			
			listar.add(cli);
		}
        return listar;
	}
}
